package com.example.Arithmetic.Arithmetic;

import com.example.Arithmetic.Lettcode.E04Lettcode145;

import java.util.LinkedList;
import java.util.List;

/**
 * 日期：2023/10/14
 * 时间：19:36
 * 描述：利用后缀表达式组成一个树
 */
public class SuffixTree {
    public static void main(String[] args) {
        String[] s = new String[]{"10", "6", "9", "3", "+", "-11", "*", "/", "*", "17", "+", "5", "+"};
        TreeNode tree = tree(s);
        System.out.println(s);
        E04Lettcode145 e04Lettcode145 = new E04Lettcode145();
        List<Integer> integerList = e04Lettcode145.preorderTraversal1(tree);
        System.out.println(integerList);
    }

    public static TreeNode tree(String[] Token) {
        LinkedList<TreeNode> list = new LinkedList();
        for (String t : Token) {
            switch (t) {
                case "+", "-", "*", "/" -> {
                    TreeNode right = list.pop();
                    TreeNode left = list.pop();
                    TreeNode treeNode = new TreeNode(t);
                    treeNode.left = left;
                    treeNode.right = right;
                    list.push(treeNode);
                }
                default -> {
                    TreeNode treeNode = new TreeNode(t);
                    list.push(treeNode);
                }
            }
        }
        return list.peek();
    }
}
